//
//  ActivityListViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "ActivityListViewController.h"
#import "AppTools.h"
#import "Movie.h"
#import "MBProgressHUD.h"
#import "ActivityListCell.h"
#import "ActDetailViewController.h"
#import "SingLeton.h"
@interface ActivityListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTabbleView;

@property (nonatomic ,retain)NSMutableArray *movieArray;

@property (nonatomic ,retain)AppTools *appTools;
@property (nonatomic ,retain)MBProgressHUD *hub;



@end

@implementation ActivityListViewController

-(void)dealloc{
    [_hub release];
    [_movieArray release];
    [_myTabbleView release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myTabbleView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTabbleView.delegate = self;
    self.myTabbleView.dataSource = self;
    [self.view addSubview:self.myTabbleView];
    [_myTabbleView release];
    [self.myTabbleView registerClass:[ActivityListCell class] forCellReuseIdentifier:@"cell"];
    self.movieArray = [NSMutableArray array];
    self.navigationItem.title = @"活动";
    //创建活动数据库
    [[SingLeton shareSingleton] openDB];
    [[SingLeton shareSingleton] createTable];
    [[SingLeton shareSingleton] selectStudent];
    //创建用户数据库
    [[SingLeton shareSingleton] openUserDB];
    [[SingLeton shareSingleton] createUserTable];
    //创建活动收藏数据库
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    [[SingLeton shareSingleton] openActDB];
    [[SingLeton shareSingleton] createActTable];
    [[SingLeton shareSingleton] selectActName:name];
    
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
#pragma mark 让菊花旋转起来
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
    self.movieArray = [SingLeton shareSingleton].array;
    
    [AppTools getDataWithURL:urlStr Block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"events"];
            [self.movieArray removeAllObjects];
            for (NSMutableDictionary *dic in array) {
                
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                
                [[SingLeton shareSingleton]insertStudent:movie];
                [movie release];
              
            }
        
            //刷新页面一定是在右数据之后刷新**************
            [self.myTabbleView reloadData];
# pragma mark　－ 菊花停止(在view刷新时)
            [self.hub hide:YES];
            
            //1.建工程,搭框架
            //2.铺页面,先用死数据
            //3.请求数据
            //4.给view赋值数据
      
    }];
        
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"cell";
   ActivityListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.myMovie = mov;
    
    return  cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  185;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ActDetailViewController *detailVC = [[ActDetailViewController alloc]init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    detailVC.myTitle = movie.title;
    detailVC.myUrl = movie.image;
    detailVC.myMold = movie.category;
    detailVC.myAddress = movie.address;
    detailVC.str = [movie.begin_time substringWithRange:NSMakeRange(5, 11)];
    detailVC.str1 = [movie.end_time substringWithRange:NSMakeRange(5, 11)];
    detailVC.myContent = movie.content;
    detailVC.myOnwer = movie.owner;
    detailVC.myImage = movie.image;
   
  
    
    
    
//    detailVC.movieID = movie.movieId;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [detailVC release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
